/**
 * Steven Cozart
 * 2011
 */

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Steven Cozart
 * @version 1.1
 * @version 8/1/11 Jon: changed my_neighbors from ArrayList<String> to Set<Territory>
 */
public class State implements Territory {
	
	/**
	 * The name of the country.
	 */
	private String my_name;
	
	/**
	 * The player currently in control the country. 
	 */
	private Player my_owner;

	/**
	 * The current army size.
	 */
	private Army my_army;
	
	/**
	 * The states connected to this state.
	 */
	private final Set<Territory> my_neighbors;

	public State(final String the_name) {
		this(the_name, null, null, null);
	}
	
	public State(final String the_name,final Player the_owner,final Collection<Territory> the_neighbors,final Army the_army){
		my_name = the_name;
		if (the_neighbors == null) {
			my_neighbors = new HashSet<Territory>();
		} else {
			my_neighbors = new HashSet<Territory>(the_neighbors);
		}
		my_owner = the_owner;
		my_army = the_army;
	}

	public State(final String the_name, final Player the_owner,final Collection<Territory> the_neighbors){
		this(the_name, the_owner, the_neighbors, null);
	}
	

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getName() {
		return my_name;
	}

	/**
	 * {@inheritDoc}
	 */
	public Army getArmy() {
		return my_army;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Player getOwner() {
		return my_owner;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean isConected(String the_conected) {// need to change string
		if(my_neighbors.contains(the_conected)){
			return true;
		}
		return false;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setOwner(Player the_player) {
		my_owner = the_player;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public int getArmySize() {
		return my_army.getSize();
	}

	@Override
	public void setArmySize(int defending) {
		my_army.setSize(defending);		
	}
	
	public String toString() {
		return my_name;
	}
	
	
	// TODO This just stores the reference.  Territory doesn't have any equals yet.
	@Override
	public void addNeighbor(final Territory the_territory) {
		if (!my_neighbors.contains(the_territory)) {
			my_neighbors.add(the_territory);
		}
		if (!the_territory.isNeighbor(this)) {
			the_territory.addNeighbor(this);
		}
	}
	
	public boolean isNeighbor(Territory the_territory) {
		return my_neighbors.contains(the_territory);
	}
}
